﻿// -----------------------------------------------------------------------
// <copyright file="ProgressWorkData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ProgressWork
    /// </summary>
    public class ProgressWorkData
    {
        /// <summary>
        /// Get ProgressWork By Id
        /// </summary>
        /// <param name="progressWorkId">Param progressWorkId</param>
        /// <returns>Return ProgressWork</returns>
        public ProgressWorkEN GetProgressWorkById(int progressWorkId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProgressWorkEN progressWork = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkGetById, progressWorkId);
                while (reader.Read())
                {
                    progressWork = new ProgressWorkEN();

                    if (!reader.IsDBNull(0))
                    {
                        progressWork.ProgressWorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        progressWork.ProfileTypeProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        progressWork.ProfileId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        progressWork.FinishDate = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        progressWork.Status = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        progressWork.Time = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        progressWork.StartDate = reader.GetDateTime(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return progressWork;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ProgressWork
        /// </summary>
        /// <returns>Return list all ProgressWork</returns>
        public IList<ProgressWorkEN> GetProgressWorks()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProgressWorkEN> lstProgressWork = new List<ProgressWorkEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkGetAll);
                while (reader.Read())
                {
                    ProgressWorkEN progressWork = new ProgressWorkEN();

                    if (!reader.IsDBNull(0))
                    {
                        progressWork.ProgressWorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        progressWork.ProfileTypeProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        progressWork.ProfileId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        progressWork.FinishDate = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        progressWork.Status = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        progressWork.Time = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        progressWork.StartDate = reader.GetDateTime(6);
                    }

                    lstProgressWork.Add(progressWork);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProgressWork;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ProgressWork
        /// </summary>
        /// <param name="progressWork">Param ProgressWork</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(ProgressWorkEN progressWork)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.ProfileTypeProgressId, progressWork.ProfileTypeProgressId),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.ProfileId, progressWork.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.FinishDate, (object)progressWork.FinishDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.Status, progressWork.Status),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.Time, progressWork.Time),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.StartDate, (object)progressWork.StartDate ?? DBNull.Value)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ProgressWork
        /// </summary>
        /// <param name="progressWork">Param ProgressWork</param>
        public void Update(ProgressWorkEN progressWork)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.ProgressWorkId, progressWork.ProgressWorkId),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.ProfileTypeProgressId, progressWork.ProfileTypeProgressId),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.ProfileId, progressWork.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.FinishDate, (object)progressWork.FinishDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.Status, progressWork.Status),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.Time, progressWork.Time),
                                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.StartDate, (object)progressWork.StartDate ?? DBNull.Value)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ProgressWork by progressWorkId
        /// </summary>
        /// <param name="progressWorkId">param progressWorkId</param>
        public void Delete(int progressWorkId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProgressWork.ProgressWorkId, progressWorkId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List ProgressWork by ProfileTypeProgressId
        /// </summary>
        /// <param name="profileTypeProgressId">Param profileTypeProgressId</param>
        /// <returns>Return List ProgressWork</returns>
        public IList<ProgressWorkEN> GetProgressWorkByProfileTypeProgressId(int profileTypeProgressId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProgressWorkEN> lstProgressWorks = new List<ProgressWorkEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkGetByProfileTypeProgressId, profileTypeProgressId);
                while (reader.Read())
                {
                    ProgressWorkEN progressWork = new ProgressWorkEN();
                    if (!reader.IsDBNull(0))
                    {
                        progressWork.ProgressWorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        progressWork.ProfileTypeProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        progressWork.ProfileId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        progressWork.FinishDate = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        progressWork.Status = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        progressWork.Time = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        progressWork.StartDate = reader.GetDateTime(6);
                    }

                    lstProgressWorks.Add(progressWork);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProgressWorks;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the progress work by profile type progress_ profile.
        /// </summary>
        /// <param name="profileTypeProgressId">The profile type progress id.</param>
        /// <param name="profileId">The profile id.</param>
        /// <returns>return ProgressWorkEN</returns>
        public ProgressWorkEN GetProgressWorkByProfileTypeProgress_Profile(int profileTypeProgressId, int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProgressWorkEN progressWork = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProgressWorkGetByProfileTypeProgress_Profile, profileTypeProgressId, profileId);
                while (reader.Read())
                {
                    progressWork = new ProgressWorkEN();

                    if (!reader.IsDBNull(0))
                    {
                        progressWork.ProgressWorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        progressWork.ProfileTypeProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        progressWork.ProfileId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        progressWork.FinishDate = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        progressWork.Status = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        progressWork.Time = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        progressWork.StartDate = reader.GetDateTime(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return progressWork;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}